﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace puissance4
{
    class Program
    {
        //  ____________________
        // /                    \
        // | VOICI LE CODE DE   |    ()-().----.          .
        // | PORC  DE SIMON,     >    \"/` ___  ;________.' 
        // | ATTENTION LES YEUX |      ` ^^   ^^
        // \____________________/

        #region void Print(char[,])
        static void Print(char[,] board)
        {
            int columns = board.GetLength(0);
            int lines = board.GetLength(1);

            Console.Write(" ");
            for (int i = 0; i < columns; i++)
                Console.Write(1 + i);
            Console.Write("\n");

            for (int i = 0; i < lines; i++)
            {
                Console.Write("|");
                for (int j = 0; j < columns; j++)
                {
                    if (board[j, i] == 'X')
                    {
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.Write("O");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    else if (board[j, i] == 'O')
                    {
                        Console.ForegroundColor = ConsoleColor.DarkCyan;
                        Console.Write("O");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    else
                        Console.Write(" ");
                }
                Console.Write("|");
                Console.Write("\n");
            }
        }

        #endregion

        static bool Play(int col, char[,] board, bool cross)
        {
            if (col >= board.GetLength(0) || col < 0)
            {
                Console.WriteLine("colonne pas dedans");
                return false;
            }
            int i = board.GetLength(1) - 1;
            while (i >= 0)
            {
                if (board[col, i] == '.')
                {
                    if (cross)
                        board[col, i] = 'X';
                    else
                        board[col, i] = 'O';
                    return true;
                }
                i--;
            }
            Console.WriteLine("colonne pleine");
            return false;

        }
        static void FillBoard(Char[,] board)
        {
            for (int i = 0; i < board.GetLength(0); i++)
                for (int j = 0; j < board.GetLength(1); j++)
                    board[i, j] = '.';
        }

        static bool IsInt(string n)
        {
            for (int i = 0; i < n.Length; i++)
                if (n[i] >= '0' || n[i] <= '9')
                    return true;

            Console.WriteLine("t'a pas rentré un entier coco");
            return false;
        }

        static bool IsWinning(char c, char[,] board)
        {
            for (int i = 0; i < board.GetLength(0); i++)
                for (int j = 0; j < board.GetLength(1) - 3; j++)
                    if (board[i, j] == c
                        && board[i, j] == board[i, j + 1]
                        && board[i, j] == board[i, j + 2]
                        && board[i, j] == board[i, j + 3])
                        return true;

            for (int i = 0; i < board.GetLength(0) - 3; i++)
                for (int j = 0; j < board.GetLength(1); j++)
                    if (board[i, j] == c
                        && board[i, j] == board[i + 1, j]
                        && board[i, j] == board[i + 2, j]
                        && board[i, j] == board[i + 3, j])
                        return true;

            for (int i = 0; i < board.GetLength(0) - 3; i++)
                for (int j = 0; j < board.GetLength(1) - 3; j++)
                    if (board[i, j] == c
                        && board[i, j] == board[i + 1, j + 1]
                        && board[i, j] == board[i + 2, j + 2]
                        && board[i, j] == board[i + 3, j + 3])
                        return true;

            for (int i = 3; i < board.GetLength(0); i++)
                for (int j = 0; j < board.GetLength(1) - 3; j++)
                    if (board[i, j] == c
                        && board[i, j] == board[i - 1, j + 1]
                        && board[i, j] == board[i - 2, j + 2]
                        && board[i, j] == board[i - 3, j + 3])
                        return true;

            return false;
        }

        //                         ____________________
        //      _____             |                    |
        //    .'     `.          <     AIE, CA PIQUE   |
        //   /  .-=-.  \   \ __   |    LES YEUX        |
        //   | (  C\ \  \_.'')    |___________________ |
        //  _\  `--' |,'   _/
        // /__`.____.'__.-'


        static void Main(string[] args)
        {
            Console.Title = "puissance 4 la puissance quatre";
            bool cross = true;
            int n = 6;
            int m = 7;
            string a;
            while (true)
            {
                Char[,] board = new char[m, n];
                FillBoard(board);
                Print(board);
                while (true)
                {
                    while (true)
                    {
                        a = Console.ReadLine();
                        if (IsInt(a))
                            if (Play(int.Parse(a) - 1, board, cross))
                                break;
                    }
                    Console.Clear();
                    Print(board);
                    if (IsWinning('X', board))
                    {
                        Console.WriteLine("BLEU A GAGNE OMG");
                        break;
                    }
                    if (IsWinning('O', board))
                    {
                        Console.WriteLine("c'est JAUNE qui gagne !");
                        break;
                    }

                    cross = !cross;
                }
            }


            //- - /  |                - - /|_/|      .--------------------.
            //   /   |  - _______________| @.@|     /      QUELLE         /
            //- |    |-- (______         >\_C/< ---/      HORREUR        /
            //  |    |  -  -   / ______  _/____)   `--------------------'
            //-- \   | -  -   / /\ \   \ \         
            // -  \  |     - (_/  \_) - \_)
            //- -  | |



        }
    }
}
